function model=update_phi(model, data, n)

%global model;

k     = model.k;
k_hat = model.k_hat;

temp=zeros(1,k_hat);
for r=1:model.r2
 tempw     = data.dataw2(r).w(n,:);
 tempwind  = find(tempw==1);
 
 for j=1:k_hat    
  temp(j) = model.alpha_2(n,j) + log(model.beta_2(r,j).beta(tempwind));
 end
 
 logsum=0;
 for j=1:k_hat   
  logsum=sumlog(temp(j),logsum);
 end

 for j=1:k_hat 
  if(logsum-temp(j)>50)
     model.phi_2(n,r,j)=model.MINVALUE;  
  else   
     model.phi_2(n,r,j)=exp(temp(j)-logsum)+model.MINVALUE;
  end
 end

 model.phi_2(n,r,:)=model.phi_2(n,r,:)/sum(model.phi_2(n,r,:));
 
end

end

